import { Bi } from "@/module-00-codebase/pkg-01-container";
import moment from "moment";
export class DateFormatMmt {
  public format(date: Date, format: string = "yyyy-MM-dd HH:mm:ss"): string | null {
    format = format.replace(/yy/g, "YY");
    format = format.replace(/dd/g, "DD");
    const res = moment(date).format(format);
    return res === "Invalid date" ? null : res;
  }
  public parse(dateStr: string, format: string = "yyyy-MM-dd HH:mm:ss"): Date | null {
    format = format.replace(/yy/g, "YY");
    format = format.replace(/dd/g, "DD");
    if (!dateStr) {
      return null;
    } else if (dateStr.length === format.length) {
      try {
        return moment(dateStr, format).toDate();
      } catch (ex: unknown) {
        return null;
      }
    } else {
      try {
        let res = new Date(dateStr);
        return Bi.utils.isNullOrEmpty(res) ? null : res;
      } catch (ex: unknown) {
        return null;
      }
    }
  }
}
